package com.campus.secondhand.controller;

import java.util.Map;

import com.campus.secondhand.model.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.campus.secondhand.common.Result;
import com.campus.secondhand.model.dto.OrderCreateDTO;
import com.campus.secondhand.model.dto.OrderReviewDTO;
import com.campus.secondhand.entity.Order;
import com.campus.secondhand.entity.OrderReview;
import com.campus.secondhand.service.OrderService;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 创建订单
    @PostMapping
    public Result<Order> createOrder(@RequestBody OrderCreateDTO orderDTO,
            @RequestParam(value = "userId") Long userId) {
        orderDTO.setBuyerId(userId); // 从认证信息中获取用户ID
        Order order = orderService.createOrder(orderDTO);
        return Result.success(order);
    }

    // 获取买家订单列表
    /**
     * 获取买家订单列表
     * 订单状态：
     * - null: 全部
     * - 1: 待交易
     * - 4: 待评价
     * - 2: 已完成
     * - 3: 已取消
     */
    @GetMapping("/buyer")
    public Result<Page<OrderVO>> getBuyerOrders(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        Page<OrderVO> orders = orderService.getBuyerOrders(userId, status, page, size);
        return Result.success(orders);
    }

    // 获取卖家订单列表
    /**
     * 获取卖家订单列表
     * 订单状态同上
     */
    @GetMapping("/seller")
    public Result<Page<OrderVO>> getSellerOrders(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        Page<OrderVO> orders = orderService.getSellerOrders(userId, status, page, size);
        return Result.success(orders);
    }

    // 获取订单详情
    @GetMapping("/{orderId}")
    public Result<Order> getOrderDetail(
            @PathVariable("orderId") Long orderId,
            @RequestParam(value = "userId") Long userId) {
        Order order = orderService.getOrderDetail(orderId, userId);
        return Result.success(order);
    }

    // 确认收货
    /**
     * 确认收货
     * 将订单状态从"待交易(1)"改为"待评价(4)"
     */
    @PostMapping("/{orderId}/confirm")
    public Result<Boolean> confirmReceipt(
            @PathVariable("orderId") Long orderId,
            @RequestParam(value = "userId") Long userId) {
        boolean success = orderService.confirmReceipt(orderId, userId);
        return Result.success(success);
    }

    // 取消订单
    @PostMapping("/{orderId}/cancel")
    public Result<Boolean> cancelOrder(
            @PathVariable("orderId") Long orderId,
            @RequestParam(value = "userId") Long userId,
            @RequestBody Map<String, String> requestBody) {
        boolean success = orderService.cancelOrder(orderId, userId, requestBody.get("reason"));
        return Result.success(success);
    }

    /**
     * 创建订单评价
     * 将订单状态从"待评价(4)"改为"已完成(2)"
     */
    @PostMapping("/{orderId}/reviews")
    public Result<OrderReview> createOrderReview(
            @PathVariable("orderId") Long orderId,
            @RequestBody OrderReviewDTO reviewDTO,
            @RequestParam(value = "userId") Long userId) {
        reviewDTO.setOrderId(orderId);
        reviewDTO.setBuyerId(userId);
        OrderReview review = orderService.createOrderReview(reviewDTO);
        return Result.success(review);
    }

    // 获取订单评价
    @GetMapping("/{orderId}/reviews")
    public Result<OrderReview> getOrderReview(
            @PathVariable("orderId") Long orderId) {
        OrderReview review = orderService.getOrderReview(orderId);
        return Result.success(review);
    }
}